package com.example.online_forum.controller;

import com.example.online_forum.common.AppResult;
import com.example.online_forum.common.ResultCode;
import com.example.online_forum.config.AppConfig;
import com.example.online_forum.exception.AppException;
import com.example.online_forum.model.Article;
import com.example.online_forum.model.Board;
import com.example.online_forum.model.User;
import com.example.online_forum.services.IArticleService;
import com.example.online_forum.services.IBoardService;
import com.sun.corba.se.spi.servicecontext.UEInfoServiceContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "文章接口" )
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IBoardService boardService;

    @PostMapping ("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("文章标题") @RequestParam("title") @NonNull String title ,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content){
        //校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            //用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //板块的校验
        Board board = boardService.selectById(boardId.longValue());
        if(board == null || board.getDeleteState() == 1 || board.getState() == 1){
            //打印日志
            //板块禁用
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        //调用Service
        articleService.creat(article);
        return AppResult.success();

    }
    @ApiOperation("查询帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("板块id") @RequestParam(value = "boardId",required = false) Long boardId){
        //查询所有
        List<Article> articles;
        if(boardId == null){
           articles = articleService.selectAll();
        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }
        if (articles == null){
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);
    }
    @ApiOperation("根据帖子id获取详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,@ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //从session中获取当前的登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用Service获取帖子详情
        Article article = articleService.selectDetailById(id);
        //如果帖子不存在
        if(article == null){
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        //判断用户是否为作者
        if(user.getId() == article.getUserId()){
            article.setOwn(true);
        }
        //返回信息
        return AppResult.success(article);
    }
    @ApiOperation("更新帖子")
    @PostMapping ("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("帖子正文") @RequestParam("content") @NonNull String content){
        //获取当前用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户状态
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article == null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断用户是不是作者
        if(user.getId() != article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //判断帖子状态
        if(article.getState() == 1 ){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //调用service
        articleService.modify(id,title,content);
        //打印日志
        log.info("帖子更新成功. Article id = " +article.getId() + ", User id = " + user.getId()+".");
        //返回正确的结果给前端
        return AppResult.success();
    }
    @ApiOperation("点赞帖子")
    @PostMapping ("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //校验用户状态
        HttpSession session = request.getSession();
        User user  = (User) session.getAttribute(AppConfig.USER_SESSION);
        //用户如果被禁言就不能点赞,只有查看操作
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        articleService.thumbUpById(id);
        return AppResult.success();
    }
    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("帖子id") @RequestParam("id") @NonNull Long id) {
        //校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        //校验帖子状态
        if(article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验用户是不是作者
        if(user.getId() != article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //调用service
        articleService.deleteById(id);
        //返回操作成功
        return AppResult.success();
    }
    @ApiOperation("根据用户id查询帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult getAllByUserId( HttpServletRequest request,
                                     @ApiParam("用户id") @RequestParam(value = "userId",required = false) Long userId){
        //从session中获取当前登录对象
        if(userId == null){
            HttpSession session =  request.getSession(false);
            User user = (User)session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        //调用service
        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}
